package newXgame.utils.list
{
	import fl.transitions.*;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	
	import imvu.magicpainter.global.object.EURLLoader;
	import imvu.magicpainter.images.imglist.struct.MenuDataStruct;
	import imvu.magicpainter.images.imglist.struct.MenuStruct;

	public class MainListStruct extends EventDispatcher
	{
		private var menuArray:Array;		//	菜单数组,记录各级菜单的状态
		private var tempArray:Array;		//	临时菜单数组,记录暂时无菜单可查询的结构		
		private var pathArray:Array;		//	要调用的地址列表		
		private var userID:String;			//	传入的用户ID

		private var m_base:String	= "";// = "http://192.168.4.27/acs_flash/";
		private var m_path:String	= "";// = "f_get_imagelist.php?uid=";

		public function MainListStruct()
		{
			menuArray = new Array();
			tempArray = new Array();
			pathArray = new Array();
		}
		//http://192.168.4.27/acs_flash/f_get_imagelist.php?uid=1435605
		//	返回树型列表
		public function get data():Array
		{
			return menuArray;
		}		
		
		//	开始读取文件内容
		public function StartLoad(uID:String, bPath:String = null, mPath:String = null):void
		{
			if(bPath.length > 0)
			{
				this.m_base = bPath;
			}
			if(mPath.length > 0)
			{
				this.m_path = mPath;
			}
		
			var m_load:EURLLoader = new EURLLoader();
			m_load.addEventListener(Event.COMPLETE, onLoadTreeDone);
			m_load.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			m_load.LoadFile(m_base + m_path + uID);
			
			userID = uID;
		}		
		
		//	对采集到的树型结构进行两次检索,取得树型结构
		private function onLoadTreeDone(e:Event):void
		{
			trace("load tree done");
			
			tempArray = new Array();
			menuArray = new Array();
			
			//	将所有传入的数据归档
			for(var i:int = 0; i < e.target.data.children().length(); i++)
			{
				var m_xml:XML = e.target.data.children()[i];

				var ms:MenuStruct = new MenuStruct();
				ms.cateID = Number(m_xml.FileId);
				ms.objID = Number(m_xml.FilePatId);
				ms.sysID = Number(m_xml.FileSysId);
				ms.objName = m_xml.FileName;
				ms.menuPath = m_xml.FileUrl;
				
				pathArray.push(ms.menuPath);
								
				if(ms.cateID == 0 && ms.objID == 0)
				{
					this.menuArray.push(ms);
				}
				else
				{
					if(this.menuArray.length > 0)
					{
						var isFind:Boolean = false;
						for(var j:int = 0; j < this.menuArray.length; j++)
						{
							var fms:MenuStruct = this.menuArray[j];
							isFind = fms.AddIteam(ms);
							
							if(isFind)
							{
								break;
							}							
						}
						
						if(!isFind)
						{
							this.tempArray.push(ms);
						}										
					}
					else
					{
						this.tempArray.push(ms);
					}
				}
			}
			
			//	对结果进行第二次检索
			if(this.tempArray.length > 0)
			{
				for(var m:int = 0; m < this.tempArray.length; m++)
				{
					var isCan:Boolean = false;
					for(var n:int = 0; n < this.menuArray.length; n++)
					{
						isCan = this.menuArray[n].AddIteam(this.tempArray[m]);
						if(isCan)
						{
							break;
						}
					}
					
					if(!isCan)
					{
						this.menuArray.push(this.tempArray[m]);
					}	
				}
			}	
			
			trace("all data is " + this.menuArray.length);			
			
			DeleteListener(e);
			LoadData();
		}
		
		//	开始读取相关内容
		private function LoadData():void
		{
			trace("start load data . last is : " + this.pathArray.length);
			
			if(this.pathArray.length > 0)
			{
				var path:String = this.pathArray.shift();				
				
				var m_load:EURLLoader = new EURLLoader();
				m_load.addEventListener(Event.COMPLETE, onLoadDataDone);
				m_load.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
				m_load.LoadFile(m_base + path + "&uid=" + userID);
			}
			else
			{
				trace("load data done...");
				this.dispatchEvent(new Event(Event.COMPLETE));
			}	
		}
		
		//	读取图像数据完毕时触发
		private function onLoadDataDone(e:Event):void
		{
			trace("load data done...");
			//	将所有传入的数据归档
			for(var i:int = 0; i < e.target.data.children().length(); i++)
			{
				var m_xml:XML = e.target.data.children()[i];
				
				var msd:MenuDataStruct = new MenuDataStruct();
				msd.objID = Number(m_xml.FileId);
				msd.cateID = Number(m_xml.FileCataId);
				msd.sysID = Number(m_xml.FileSysId);
				msd.objName = m_xml.FileName;
				msd.imgWidth = Number(m_xml.FileWidth);
				msd.imgHeight = Number(m_xml.FileHeight);
				msd.imgUrl = m_xml.FileUrl;
				msd.imgSubUrl = m_xml.FileSubUrl;
				msd.isPublic = Boolean(Number(m_xml.FileIsPublic));
				msd.isAp = Boolean(Number(m_xml.FileIsAp));
				msd.imgMemo = m_xml.FileDescription;
				
				for(var j:int = 0; j < this.menuArray.length; j++)
				{
					var ms:MenuStruct = this.menuArray[j];
					if(ms.AddData(msd))
					{
						break;
					}					
				}
			}			
			
			DeleteListener(e);			
			LoadData();
		}
		
		//	删除对象的事件侦听
		private function DeleteListener(e:Event):void
		{
			e.target.removeEventListener(Event.COMPLETE, onLoadTreeDone);
			e.target.removeEventListener(Event.COMPLETE, onLoadDataDone);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
		}
		
		//	删除事件侦听
		private function onLoadError(e:*):void
		{
			trace("load data error!!");
			
			DeleteListener(e);
			
			LoadData();
		}		
	}
}